﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Oplaco.Web.DTO;

public partial class Controls_CategoryDropDownList : System.Web.UI.UserControl
{
    public int SelectedCategoryId { get; set; }
    public int SelectedChildCategoryId { get; set; }
    public string InitialListItem { get; set; }
    public string TopCategoryLabel { get; set; }
    public string SubCategoryLabel { get; set; }

    public void Refresh() {
        CategoryManager.ClearCache(HttpContext.Current);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(TopCategoryLabel)) lblTopCatTitle.Visible = true; lblTopCatTitle.Text = TopCategoryLabel;
        

        if (!Page.IsPostBack) {
            this.loadCategoryList();
            if (this.SelectedChildCategoryId > 0)
            {                
                this.loadChildCategoryList();                
            }
        }
    }

    /// <summary>
    /// Processes postback call and tries to load subcategories
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void CategoryListIndexChanged(object sender, EventArgs e) {

        if (!String.IsNullOrEmpty(SubCategoryLabel)) lblSubCatTitle.Visible = true; lblSubCatTitle.Text = SubCategoryLabel;
        this.SelectedCategoryId = Int32.Parse(ddlCategories.SelectedValue);
        this.loadChildCategoryList();
        
    }

    protected void ChildCategoryListIndexChanged(object sender, EventArgs e)
    {
        ListItemCollection coll = new ListItemCollection();
        coll.Add(new ListItem("--- select ---", "-1"));
        if (ddlCategories.SelectedIndex <= 0) {
            ddlSubCategories.DataSource = coll;
            ddlSubCategories.DataBind();
        }

    }

    /// <summary>
    /// 
    /// </summary>
    private void loadCategoryList()
    {
        ddlCategories.DataTextField = "Text";
        ddlCategories.DataValueField = "Value";

        CachedCategoryCollection categories = CategoryManager.Fetch(HttpContext.Current, 100);
        if (categories != null && categories.Count() > 0)
        {
            List<ListItem> list = (from item in categories select new ListItem(item.Title, item.Id.ToString())).ToList<ListItem>();
            list.Insert(0, new ListItem("--- Select ---", "-1"));


            ddlCategories.DataSource = list;
            ddlCategories.DataBind();

            //check if some id has been set,...
            if (this.SelectedCategoryId > 0) ddlCategories.SelectedValue = SelectedCategoryId.ToString();
            else  ddlCategories.SelectedIndex = 0;
        }
    }

    private void loadChildCategoryList() {
        ListItemCollection coll = new ListItemCollection();
        CachedCategoryCollection categories = CategoryManager.FetchByParentId(HttpContext.Current, 100, this.SelectedCategoryId);
        if (categories != null && categories.Count() > 0)
        {
            ddlSubCategories.DataTextField = "Text";
            ddlSubCategories.DataValueField = "Value";

            ListItem[] list = null;
            list = (from item in categories select new ListItem(item.Title, item.Id.ToString())).ToArray<ListItem>();
            coll.Add(new ListItem("--- select ---", "-1"));
            coll.AddRange(list);

            ddlSubCategories.DataSource = coll;
            ddlSubCategories.DataBind();
            ddlSubCategories.Enabled = true;
            ddlSubCategories.Visible = true;
            if (this.SelectedChildCategoryId > 0) ddlSubCategories.SelectedValue = this.SelectedChildCategoryId.ToString();
        }

        if (ddlCategories.SelectedIndex <= 0)
        {

            coll.Add(new ListItem("--- Select ---", "-1"));
            if (ddlCategories.SelectedIndex <= 0)
            {
                ddlSubCategories.DataSource = coll;
                ddlSubCategories.DataBind();
            }
        }
       
    }
}